#include "json_device_object.h"
#include "control/cswitch.h"
#include "control/cwindow.h"
#include "control/cscene.h"
#include "control/cdimming.h"
#include "control/cairdim.h"
#include "control/cairmode.h"
#include "control/cairwind.h"
#include "control/clabel.h"
#include "control/cmultimedia.h"
#include "knx/cmei.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

Json_DEVICE_OBJECT::Json_DEVICE_OBJECT(QByteArray json, void *cntDelegate)
{
    _regList = NULL;
    _usrcontrol = NULL;
    _read_times = 0;

    QJsonDocument jd = QJsonDocument::fromJson(json);
    if(jd.isObject())
    {
        QJsonObject obj = jd.object();
        _pid = obj.value("projectId").toString();
        _id = obj.value("deviceId").toString();
        _type = obj.value("type").toString();
        _name = obj.value("name").toString();
        _exp = obj.value("exp").toString();
        _action = obj.value("action").toString();
        _feedback = obj.value("feedback").toString();
        _send = obj.value("send").toString();
        _state_value =obj.value("stateValue").toString();

        if(_type == "switch")
        {
            _usrcontrol = new CSwitch();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "scene")
        {
            _usrcontrol = new CScene();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "dim")
        {
            _usrcontrol = new CDimming();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "window")
        {
            _usrcontrol = new CWindow();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "air_dim")
        {
            _usrcontrol = new CAirDim();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "air_mode")
        {
            _usrcontrol = new CAirMode();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "air_wind")
        {
            _usrcontrol = new CAirWind();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "label")
        {
            _usrcontrol = new CLabel();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->setKnxObject(this);
            _usrcontrol->cntDelegate = cntDelegate;
        }
        else if(_type == "media")
        {
            _usrcontrol = new Cmultimedia();
            _usrcontrol->parseExp(_exp);
            _usrcontrol->cntDelegate = cntDelegate;

            QJsonDocument jp = QJsonDocument::fromJson(_action.toLocal8Bit());

            if(jp.isArray())
            {
                QJsonArray array = jp.array();
                _usrcontrol->setKnxObject(&array);
            }

        }

        knxRegListRequest();
    }
}

Json_DEVICE_OBJECT::~Json_DEVICE_OBJECT()
{
    removeDevList();
}

void Json_DEVICE_OBJECT::doAction(QString action,QString value)
{
    if(_usrcontrol)
        _usrcontrol->execKnxAction(action,value);
}

void Json_DEVICE_OBJECT::doRead()
{
    if(_usrcontrol)
    {
        _usrcontrol->readKnxAction();
        _read_times ++;
    }
}

void Json_DEVICE_OBJECT::removeDevList()
{
    if(_regList!=NULL)
    {
        for(int i=0;i<_regList->size();i++)
        {
            delete _regList->at(i);
        }

        _regList->clear();
        _regList = NULL;
    }
}

QList<DEVICE_OBJECT *> * Json_DEVICE_OBJECT::knxRegListRequest()
{
    removeDevList();

    if(_feedback.isEmpty())
        return NULL;

    _regList = new QList<DEVICE_OBJECT *>();

    QStringList feedbackList = _feedback.split(";");
    for(int i=0;i<feedbackList.size();i++)
    {
        QStringList fl = feedbackList.at(i).split(":");
        if(fl.size()>=3)
        {
            DEVICE_OBJECT *d = new DEVICE_OBJECT();

            d->_name = _id;
            d->_address = fl.at(0).toUtf8();
            d->_data_type = (DATA_TYPE)fl.at(1).toUInt();
            d->_data_define = (DATA_DEFINE)fl.at(2).toUInt();
            d->_control = _type;

            if(d->_address.isEmpty())
                continue;

            cmei_set_group_address(d->_ga_ia,d->_address);

            _regList->append(d);
        }
    }

    return _regList;
}

QString Json_DEVICE_OBJECT::actionFromStatus(QString address,QString value)
{
    if(_usrcontrol)
    {
        return _usrcontrol->actionFromStatus(address,value);
    }

    return "";
}

QString Json_DEVICE_OBJECT::valueCheck(QString action,QString value)
{
    if(_usrcontrol)
    {
        return _usrcontrol->valueCheck(action,value);
    }

    return value;
}
